<?php

class TableManager {

    private static $me = NULL;

    /**
     *
     * @return TableManger
     */
    public static function getInstance() {

        if (!self::$me instanceof TableManager) {
            self::$me = new TableManager();
        }

        return self::$me;
    }

    public function __construct() {
        NULL;
    }

    public function getTable($tableID) {
        return new Table($tableID);
    }

    public function getTableFields($tableID) {
        $query = new Query();
        $query->select('ID');
        $query->from('fields');
        $query->where('tableID', $tableID);
        $query->build('SELECT');
        $query->exec();
        $IDs = array();

        while ($fieldID = $query->fetchField('ID')) {
            $IDs[] = $fieldID;
        }
        return $IDs;
    }

    public function addTable() {
        try {

            $name = $_REQUEST['table_name'];
            $source = $_REQUEST['table_source'];
            $physicalName = $_REQUEST['table_physical_name'];
            $contents = $_REQUEST['table_contents'];
            $length = $_REQUEST['table_length'];
            $description = $_REQUEST['table_description'];
            $table = new Table();
            $table->setName($name);
            $table->setSource($source);
            $table->setPhysicalName($physicalName);
            $table->setContents($contents);
            $table->setLength($length);
            $table->setDescription($description);
            $table->add();
            $success = true;
            $message = 'Information has been added successfully';
            $content = array();
            $content ['tableID'] = $table->getID();
        } catch (Exception $e) {
            $success = false;
            $message = $e->getMessage();
            $content = '';
        }
        return Utility::buildSuccessFailXmlResponse($success, $message, $content);
    }

    public function updateTable() {

        try {
            $tableID = $_REQUEST['table_id'];
            $name = $_REQUEST['table_name'];
            $source = $_REQUEST['table_source'];
            $physicalName = $_REQUEST['table_physical_name'];
            $contents = $_REQUEST['table_contents'];
            $length = $_REQUEST['table_length'];
            $description = $_REQUEST['table_description'];
            $table = new Table($tableID);
            $table->setName($name);
            $table->setSource($source);
            $table->setPhysicalName($physicalName);
            $table->setContents($contents);
            $table->setLength($length);
            $table->setDescription($description);
            $table->update();
            $success = true;
            $message = 'Information has been updated successfully';
            $content ['tableID'] = $table->getID();
        } catch (Exception $e) {
            $success = false;
            $message = $e->getMessage();
            $content = '';
        }
        return Utility::buildSuccessFailXmlResponse($success, $message, $content);
    }

    public function deleteTable($tableID) {
        try {
            $table = new Table($tableID);
            $table->delete();
            $success = true;
            $message = 'Information has been deleted successfully';
        } catch (Exception $e) {
            $success = false;
            $message = $e->getMessage();
        }
        return Utility::buildSuccessFailXmlResponse($success, $message);
    }

    public function exportTable() {
        
    }

    public function getAllTables() {
        $query = new Query();

        $query->select("ID");
        $query->from('tables');
        $query->build('SELECT');
        $query->exec();

        $IDs = array();

        while ($tableID = $query->fetchField('ID')) {
            $IDs[] = $tableID;
        }
        return $IDs;
    }

    public function getTableListView() {
        return TableListView::getInstance()->getTableListTemplate($this->getAllTables());
    }

    public function getEditTableView($tableID=0) {
        return TableEditView::getInstance()->getTableEditTemplate($tableID);
    }
    public function getViewTableView($tableID){
        
        return TableViewView::getInstance()->getTableViewTemplate($tableID);
    }

}

?>
